package cfg

import (
	"encoding/json"
	"fmt"
	"os"
	"runtime"
	"time"
)

type password struct {
	MySQL        accountAndPassword `json:"mysql"`
	Redis        accountAndPassword `json:"redis"`
	MongoDB      accountAndPassword `json:"mongo_db"`
	JwtSecretKey string             `json:"jwt_secret_key"`
	PwdSecretKey string             `json:"pwd_secret_key"`
}

type accountAndPassword struct {
	IP       string `json:"ip"`
	Account  string `json:"account"`
	Password string `json:"password"`
	Port     uint   `json:"port"`
	Other    string `json:"other"`
}

var Password password

func init() {
	switch runtime.GOOS {
	case "windows":
		file, err := os.ReadFile(`c:\password.json`)
		if err != nil {
			fmt.Println(`读取 c:\password.json 文件失败。`)
			time.Sleep(time.Second * 3)
			panic(err)
		}
		err = json.Unmarshal(file, &Password)
		if err != nil {
			fmt.Println(`json 解码 c:\password.json 文件失败。`)
			time.Sleep(time.Second * 3)
			panic(err)
		}
	default:
		file, err := os.ReadFile(`/root/password.json`)
		if err != nil {
			fmt.Println(`读取 /root/password.json 文件失败。`)
			time.Sleep(time.Second * 3)
			panic(err)
		}
		err = json.Unmarshal(file, &Password)
		if err != nil {
			fmt.Println(`json 解码 /root/password.json 文件失败。`)
			time.Sleep(time.Second * 3)
			panic(err)
		}
	}
}
